/**
 * 自定义错误类
 */
export class AppError extends Error {
  public statusCode: number;
  public isOperational: boolean;

  constructor(message: string, statusCode: number = 500, isOperational: boolean = true) {
    super(message);
    
    this.statusCode = statusCode;
    this.isOperational = isOperational;
    
    // 确保错误堆栈正确显示
    Error.captureStackTrace(this, this.constructor);
  }
}

/**
 * 验证错误
 */
export class ValidationError extends AppError {
  constructor(message: string) {
    super(message, 400);
  }
}

/**
 * 认证错误
 */
export class AuthenticationError extends AppError {
  constructor(message: string = '认证失败') {
    super(message, 401);
  }
}

/**
 * 授权错误
 */
export class AuthorizationError extends AppError {
  constructor(message: string = '权限不足') {
    super(message, 403);
  }
}

/**
 * 资源未找到错误
 */
export class NotFoundError extends AppError {
  constructor(message: string = '资源未找到') {
    super(message, 404);
  }
}

/**
 * 冲突错误
 */
export class ConflictError extends AppError {
  constructor(message: string = '资源冲突') {
    super(message, 409);
  }
}

/**
 * 数据库错误
 */
export class DatabaseError extends AppError {
  constructor(message: string = '数据库操作失败') {
    super(message, 500);
  }
}

/**
 * 外部服务错误
 */
export class ExternalServiceError extends AppError {
  constructor(message: string = '外部服务错误') {
    super(message, 502);
  }
}

/**
 * 速率限制错误
 */
export class RateLimitError extends AppError {
  constructor(message: string = '请求过于频繁') {
    super(message, 429);
  }
}

/**
 * 业务逻辑错误
 */
export class BusinessError extends AppError {
  constructor(message: string, statusCode: number = 400) {
    super(message, statusCode);
  }
}

/**
 * 错误工厂函数
 */
export const createError = {
  validation: (message: string) => new ValidationError(message),
  authentication: (message?: string) => new AuthenticationError(message),
  authorization: (message?: string) => new AuthorizationError(message),
  notFound: (message?: string) => new NotFoundError(message),
  conflict: (message?: string) => new ConflictError(message),
  database: (message?: string) => new DatabaseError(message),
  externalService: (message?: string) => new ExternalServiceError(message),
  rateLimit: (message?: string) => new RateLimitError(message),
  business: (message: string, statusCode?: number) => new BusinessError(message, statusCode),
  app: (message: string, statusCode?: number) => new AppError(message, statusCode)
};

/**
 * 错误处理工具函数
 */
export const errorUtils = {
  /**
   * 判断是否为操作性错误
   */
  isOperationalError: (error: Error): boolean => {
    if (error instanceof AppError) {
      return error.isOperational;
    }
    return false;
  },

  /**
   * 获取错误状态码
   */
  getStatusCode: (error: Error): number => {
    if (error instanceof AppError) {
      return error.statusCode;
    }
    return 500;
  },

  /**
   * 格式化错误响应
   */
  formatErrorResponse: (error: Error, includeStack: boolean = false) => {
    const response: any = {
      success: false,
      message: error.message,
      timestamp: new Date().toISOString()
    };

    if (error instanceof AppError) {
      response.statusCode = error.statusCode;
    }

    if (includeStack && error.stack) {
      response.stack = error.stack;
    }

    return response;
  }
};